<template>
  <el-dialog
    v-model="visible"
    title="批量确认入库"
    width="600px"
    :close-on-click-modal="false"
  >
    <div class="batch-confirm-content">
      <div class="info-section">
        <p>已选择 <strong>{{ selectedCount }}</strong> 个资产进行批量确认入库</p>
      </div>

      <el-form ref="formRef" :model="form" label-width="100px">
        <el-form-item label="责任部门">
          <el-select
            v-model="form.responsible_department"
            placeholder="请选择责任部门（可选）"
            clearable
            filterable
            style="width: 100%"
          >
            <el-option
              v-for="dept in props.departmentOptions || []"
              :key="dept.value"
              :label="dept.label"
              :value="dept.value"
          />
          </el-select>
        </el-form-item>
        <el-form-item label="责任人">
          <el-input
            v-model="form.responsible_person"
            placeholder="请输入责任人（可选）"
          />
        </el-form-item>
        <el-form-item label="确认原因">
          <el-input
            v-model="form.confirm_reason"
            type="textarea"
            :rows="4"
            placeholder="请输入确认原因（可选）"
          />
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="submitting" @click="handleSubmit">
        确认
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from "vue";
import type { FormInstance } from "element-plus";

const props = defineProps({
  modelValue: { type: Boolean, default: false },
  selectedCount: { type: Number, default: 0 },
  departmentOptions: {
    type: Array as () => Array<{ label: string; value: string }>,
    default: () => [],
  },
});

const emit = defineEmits<{
  (e: "update:modelValue", value: boolean): void;
  (e: "confirm", data: {
    responsible_department?: string;
    responsible_person?: string;
    confirm_reason?: string;
  }): void;
}>();

const visible = computed({
  get: () => props.modelValue,
  set: (value: boolean) => emit("update:modelValue", value),
});

const formRef = ref<FormInstance>();
const submitting = ref(false);
const form = ref({
  responsible_department: "",
  responsible_person: "",
  confirm_reason: "",
});

// 监听弹窗打开，初始化表单
watch(visible, (newVal) => {
  if (newVal) {
    form.value = {
      responsible_department: "",
      responsible_person: "",
      confirm_reason: "",
    };
  }
});

// 提交
const handleSubmit = () => {
  const submitData: any = {};
  if (form.value.responsible_department) {
    submitData.responsible_department = form.value.responsible_department;
  }
  if (form.value.responsible_person) {
    submitData.responsible_person = form.value.responsible_person;
  }
  if (form.value.confirm_reason) {
    submitData.confirm_reason = form.value.confirm_reason;
  }
  emit("confirm", submitData);
};

// 取消
const handleCancel = () => {
  emit("update:modelValue", false);
};
</script>

<style scoped lang="scss">
.batch-confirm-content {
  .info-section {
    margin-bottom: 20px;
    padding: 10px;
    background-color: #f5f7fa;
    border-radius: 4px;

    p {
      margin: 0;
      color: #606266;

      strong {
        color: #409eff;
      }
    }
  }
}
</style>

